/***************************************************************************************
Firm-embedded productivity and cross-country income differences
Alviarez, Cravino and Ramondo
Journal of Political Economy (2022)

Program: table_A3_N_R2_MSE.do
Date: October 2022

Description: Reproduces Table A.3: Number of observations, R-squared, and mean squared errors.

*****************************************************************************************/

*-------------------------------------------------------------------------------
global typeden=1
include "set_directories.do"
set memory 64g
global lf "LF"
	
*Log
cap log close
log using "${clogs}/table_A3_N_R2_MSE.log",replace
*-------------------------------------------------------------------------------



*------------------------------------------------------------------------------
*Generate Table A3
*------------------------------------------------------------------------------
clear all
set obs 100

gen model=""
gen num_obs=. 
gen R2=. 
gen rms=. 
tempfile temp
save `temp', replace 


local iter=0

foreach rsample in "naics_sales_s1_base_woparent" "naics_emp_s1_base_woparent_salesempva" "naics_va_s1_base_woparent_salesempva" "naics_sales_s1_base_withparent" "naics_sales_s1_base_woparent_decile_2_9" "naics_sales_s1_base_woparent_decile_3_8" "naics_sales_s1_base_woparent_decile_4_7" "naics_sales_s1_base_woparent_decile_5_6" "naics_sales_s1_base_woparent_above3ctry" "naics_sales_s1_base_woparent_above5ctry" "naics_sales_s1_base_woparent_above10ctry" "naics4_sales_s1_base_woparent"  "naics_sales_s1_base_woparent_exclnomark" "naics_sales_s1_base_woparent_ctte" "naics_sales_s1_base_woparent_aboveage10" "naics_sales_s1_base_woparent_gdpdiff" "naics_sales_s1_base_woparent_tax_ratio" "naics_sales_s1_base_woparent_BTT" "naics_sales_s1_base_woparent_nogravity"  {

display "`rsample'"
clear all  
local iter=`iter'+1
display "`iter'"

use "${data}/R2_`rsample'.dta", clear
sum num_obs
global num_obs_`iter'=`r(mean)'
display "${num_obs_`iter'}"
sum rms
global rms_`iter'=`r(mean)'
sum R2
global R2_`iter'=`r(mean)'
display "${R2_`iter'}"

use `temp', clear
replace model="`rsample'" if _n==`iter'
replace num_obs=${num_obs_`iter'} if _n==`iter'
replace rms=${rms_`iter'} if _n==`iter'
replace R2=${R2_`iter'} if _n==`iter'
replace model="Baseline" if _n==1

format %9.2fc rms R2
format %9.0fc num_obs
save, replace 

}

drop if model==""

*Export table to excel
export delimited using "${rappendix}/table_A3_N_R2_MSE.csv", datafmt replace

log close